﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Project_V1
{
    public partial class FrmMaintenance : Form
    {
        public FrmMaintenance()
        {
            InitializeComponent();
            this.Load += FrmMaintenance_Load;
        }
        private void tb_PadNum_Click(object sender, EventArgs e)
        {
            TextBox tb_temp = (TextBox)sender;
            FrmKeyPadNum.input = tb_temp.Text;
            // 获取 TextBox 的屏幕位置
            Point textboxLocation = tb_temp.PointToScreen(Point.Empty);
            // 创建一个新的窗体
            FrmKeyPadNum frmSoftKey = new FrmKeyPadNum(tb_temp.Text);
            // 计算窗体的位置
            //textboxLocation.X = 1;
            //textboxLocation.Y = 1;
            int popupX = textboxLocation.X;
            int popupY = textboxLocation.Y;
            // 设置窗体的位置
            frmSoftKey.Location = new Point(popupX, popupY);
            // 显示窗体
            frmSoftKey.ShowDialog();
            //MessageBox.Show("显示键盘之后");
            //MessageBox.Show(FrmSoftKey.input);
            if (FrmKeyPadNum.confirm)
            {
                tb_temp.Text = FrmKeyPadNum.input;
                tb_temp.SelectionStart = tb_temp.Text.Length;
                tb_temp.SelectionLength = 0;
                tb_temp.Focus();
                Beckhoff_PLC.WriteVariable("_03_HMI.FeedMachModel", Int16.Parse(txt_MachineType.Text));
                Toexcel.WriteMachineType(txt_MachineType.Text, System.Environment.CurrentDirectory + "\\ConfigFile\\AmsNetID.csv"); 
                Beckhoff_PLC.MachineType = Toexcel.ReadMachineType(System.Environment.CurrentDirectory + "\\ConfigFile\\AmsNetID.csv");
                CommonMethods.CurrentValue["_03_HMI.FeedMachModel"] = txt_MachineType.Text;
                DataSync.SavePowerOffValueToCSV(System.Environment.CurrentDirectory + "\\ConfigFile\\Cache.csv");
            }
        }
        #region 打开窗体方法,和主界面的方法一样
        private void OpenWindow(Form Frm)
        {
            Frm.TopLevel = false;
            Frm.FormBorderStyle = FormBorderStyle.None;
            Frm.Dock = DockStyle.Fill;
            Frm.Parent = this.MainPanel;
            Frm.Show();
        }

        private bool CloseWindow(string FrmName)
        {
            foreach (Control ct in this.MainPanel.Controls)
            {
                if (ct is Form)
                {
                    Form Frm = (Form)ct;
                    if (Frm.Name == FrmName)
                    {
                        return true;
                    }
                    else
                    {
                        Frm.Close();
                    }
                }
            }
            return false;
        }
        #endregion

        private void FrmMaintenance_Load(object sender, EventArgs e)
        {
            Beckhoff_PLC.MachineType = Toexcel.ReadMachineType(System.Environment.CurrentDirectory + "\\ConfigFile\\AmsNetID.csv");
            txt_MachineType.Text = Beckhoff_PLC.MachineType;

            Beckhoff_PLC.WriteVariable("_03_HMI.btMotorDebug", false);
            Beckhoff_PLC.WriteVariable("_03_HMI.btFeedTest", false);
            this.btn_MotorTest_Click(null, null);
        }

        private void btn_MotorTest_Click(object sender, EventArgs e)
        {
            //if (CloseWindow("FrmMotorTest") == false)
            //{
            //    OpenWindow(new FrmMotorTest());
            //}
            if (CloseWindow("FrmDebug") == false)
            {
                OpenWindow(new FrmDebug());
            }


            Beckhoff_PLC.IsWriting = true;
            //if (CommonMethods.CurrentValue["_03_HMI.btMotorDebug"] == "False")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btMotorDebug", true);
                Beckhoff_PLC.WriteVariable("_03_HMI.btFeedTest", false);
            }
            //if (CommonMethods.CurrentValue["_03_HMI.btMotorDebug"] == "True")
            //{

            //    Beckhoff_PLC.WriteVariable("_03_HMI.btMotorDebug", false);
            //    Beckhoff_PLC.WriteVariable("_03_HMI.btFeedTest", true);
            //}
            Beckhoff_PLC.IsWriting = false;



        }

        private void btn_Calibration_Click(object sender, EventArgs e)
        {
            if (CloseWindow("FrmCalibration") == false)
            {
                OpenWindow(new FrmCalibration());
            }
        }

        private void btn_SysDebug_Click(object sender, EventArgs e)
        {
            if (CloseWindow("FrmSysDebug") == false)
            {
                OpenWindow(new FrmSysDebug());
            }

            Beckhoff_PLC.IsWriting = true;
            if (CommonMethods.CurrentValue["_03_HMI.btFeedTest"] == "False")
            {
                // btn_MotorTest.Enabled = false;
                Beckhoff_PLC.WriteVariable("_03_HMI.btFeedTest", true);

                Beckhoff_PLC.WriteVariable("_03_HMI.btMotorDebug", false);
            }
            if (CommonMethods.CurrentValue["_03_HMI.btFeedTest"] == "True")
            {
                // btn_MotorTest.Enabled = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btFeedTest", false);

                Beckhoff_PLC.WriteVariable("_03_HMI.btMotorDebug", true);
            }
            Beckhoff_PLC.IsWriting = false;


        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            foreach (Control ct in LeftPanel.Controls)
            {
                if (ct is Button && ct.Tag != null && ct.Tag.ToString() != "" && ct.Tag.ToString().IndexOf(".") != -1)
                {

                    if (ct.Tag.ToString() == "_03_HMI.btMotorDebug")
                    {
                        if (CommonMethods.CurrentValue[ct.Tag.ToString()] == "True" && CommonMethods.CurrentValue.ContainsKey(ct.Tag.ToString()))
                        {
                            ct.BackColor = Color.FromArgb(60, 179, 113);
                        }
                        else
                        {
                            ct.BackColor = Color.FromArgb(255, 255, 255);
                        }
                    }

                    if (ct.Tag.ToString() == "_03_HMI.btFeedTest")
                    {

                        if (CommonMethods.CurrentValue[ct.Tag.ToString()] == "True" && CommonMethods.CurrentValue.ContainsKey(ct.Tag.ToString()))
                        {
                            ct.BackColor = Color.FromArgb(60, 179, 113);
                        }
                        else
                        {
                            ct.BackColor = Color.FromArgb(255, 255, 255);
                        }
                    }
                }
            }
        }

        private void LeftPanel_Paint(object sender, PaintEventArgs e)
        {

        }

        private void MainPanel_Paint(object sender, PaintEventArgs e)
        {

        }

        private void txt_MachineType_Enter(object sender, EventArgs e)
        {

            //Toexcel.ReadMachineID(System.Environment.CurrentDirectory + "\\ConfigFile\\AmsNetID.csv");
        }

        private void txt_MachineType_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {



                Beckhoff_PLC.WriteVariable("_03_HMI.FeedMachModel", Int16.Parse(txt_MachineType.Text));

                Toexcel.WriteMachineType(txt_MachineType.Text, System.Environment.CurrentDirectory + "\\ConfigFile\\AmsNetID.csv");

                Beckhoff_PLC.MachineType = Toexcel.ReadMachineType(System.Environment.CurrentDirectory + "\\ConfigFile\\AmsNetID.csv");






            }
        }
    }
}
